package edu.neepu;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class test3 {



    public void go() throws IOException, InterruptedException
    {
        int i=0;
        String name = "";
        int j =0;
        for (i=5;i<7;i++){
            Document document = Jsoup.connect("https://bing.ioliu.cn/?p=" + i).get();
            Elements elements = document.select(".item");
            for (Element element : elements) {
                Elements d = element.select(".description");
                System.out.println("名字 "+d.text());
                name = d.text();
                Elements a = element.select(".ctrl.download");
                System.out.println("下载链接 "+a.attr("abs:href"));
                Elements b = element.select(".calendar");
                System.out.println("拍摄时间"+b.text());
                Elements c = element.select(".location");
                System.out.println("拍摄位置"+c.text());
                System.out.println("     ");
                name = b.text()+c.text();
                File files = new File("C:\\shixun2\\");
                if (!files.exists()) {
                    files.mkdirs();
                }
                //获取下载地址
                URL url = new URL(a.attr("abs:href"));
                //链接网络地址
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                //获取链接的输出流
                InputStream is = connection.getInputStream();
                //创建文件，fileName为编码之前的文件名
                File file = new File("C:\\shixun2\\"+name+".jpg"  );
                j++;
                //根据输入流写入文件
                FileOutputStream out = new FileOutputStream(file);
                int p = 0;
                while((p = is.read()) != -1){
                    out.write(p);
                }
                out.close();
                is.close();
            }
        }
    }
}
